﻿String.prototype.trim=function(){return this.replace(/$\s*|\s*$/g,'');}
var Navy={author:'Navy',homepage:'http://www.shopwork.net'};
//获取json对象
Navy.getJson=function(v){if(typeof(v)=='string')return eval('('+v+')');else return v;}
//根据id获取对象
Navy.$=function(Id){if('object'==typeof(Id))return Id;else if('string'==typeof(Id))return document.getElementById(Id);else return null;}
Navy.IsIE=!!document.all;
//ie浏览器版本
Navy.IEVersion=(function(){if(!Navy.IsIE)return -1;try{return parseFloat(/msie ([\d\.]+)/i.exec(navigator.userAgent)[1]);}catch(e){return -1;}})();
//扩展IE下的XMLHttpRequest
if(Navy.IsIE&&!window.XMLHttpRequest)window.XMLHttpRequest=function(){
  var acX=['msxml2.xmlhttp.5.0','msxml2.xmlhttp.4.0','msxml2.xmlhttp.3.0','msxml2.xmlhttp','microsoft.xmlhttp'],Xhr;
  for(var i=0;i<acX.length;i++)try{Xhr=new ActiveXObject(acX[i]);return Xhr;}catch(e){}
  return false;
}
//ajax应用池
Navy.Ajax={
  pools:[]//注意pools存储的对象为{xhr:ajax对象,status:ajax的状态}，其中ajax的状态为1/0，1表示在使用中，0表示readyState==4了并且执行了回调函数
  ,getObject:function(){
     for(var i=0;i<this.pools.length;i++)if(this.pools[i].status===0){
       this.pools[i].status=1;//设置为使用状态
       this.pools[i].xhr.onreadystatechange=function(){}//删除状态转换函数
       this.pools[i].xhr.abort();//调用abort
       return this.pools[i];
     }
     var xhr=new XMLHttpRequest();
     if(xhr.readyState==null){//更正某些Mozilla浏览器无readyState的问题
       xhr.readyState=0;
       xhr.addEventListener("load",function(){
         xhr.readyState=4;
         if(typeof(xhr.onreadystatechange)=="function")xhr.onreadystatechange();
       },false);
     }
     this.pools[this.pools.length]={xhr:xhr,status:1};
     return this.pools[this.pools.length-1];
  }
  ,send:function(cfg){/*cfg示例  
    {
     url:'请求的页面'
    ,params:'键值对，注意不是json对象'
    ,method:'post/get，如果为指定则默认为get'
    ,success:成功时的回调函数
    ,failure:失败时的回调函数
    ,otherParams:提供给回调函数的其他参数，可以为json对象
    }
    
    成功或者失败的回调函数参数为  (当前的xhr对象，配置文件的中的otherParams)
    */
     if(!cfg||!cfg.url)throw("url不正确！");
     var method=cfg.method,asy="boolean"==typeof(cfg.asy)?cfg.asy:true;
     if(!method)method="get";
     if(method.toLocaleLowerCase()=='get'){
         var _dc=new Date().getTime();
         cfg.params=cfg.params?cfg.params+'&_dc='+_dc:'_dc='+_dc;
         if(cfg.url.indexOf("?")!=-1)cfg.url+="&"+cfg.params;
         else cfg.url+="?"+cfg.params;
         cfg.params=null;
     }
     else if(typeof(cfg.params)=="undefined")cfg.params='';
     var o=this.getObject();//注意并非实际的xhr对象
     if(!o.xhr)throw("未能创建ajax对象！");
     o.xhr.open(method,cfg.url,asy);
     if(method.toLocaleLowerCase()=='post')o.xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");    
     o.xhr.onreadystatechange=function(){
       if(o.xhr.readyState==4){
          if(o.xhr.status==200||o.xhr.status==0){
            if("function"==typeof(cfg.success))cfg.success(o.xhr,cfg.otherParams);            
          }
          else if("function"==typeof(cfg.failure))cfg.failure(o.xhr,cfg.otherParams);
          o.status=0;//=============更改状态为未使用
       }
     }
     o.xhr.send(cfg.params);
  }
}
//设置cookie
Navy.SetCookie=function(name,value,icfgTime){var cfgTime={};if("undefined"==typeof(icfgTime))cfgTime.y=1;else cfgTime=icfgTime; var d=new Date();if(Navy.IsNumeric(cfgTime.y))d.setFullYear(d.getFullYear()+cfgTime.y);if(Navy.IsNumeric(cfgTime.M))d.setMonth(d.getMonth()+cfgTime.M);if(Navy.IsNumeric(cfgTime.d))d.setDate(d.getDate()+cfgTime.d);if(Navy.IsNumeric(cfgTime.h))d.setHours(d.getHours()+cfgTime.h);if(Navy.IsNumeric(cfgTime.m))d.setMinutes(d.getMinutes()+cfgTime.m);if(Navy.IsNumeric(cfgTime.s))d.setSeconds(d.getSeconds()+cfgTime.s);document.cookie=name+"="+value+";expires="+d.toGMTString();}
//获取cookie
Navy.GetCookie=function(name){var m=new RegExp(name+'=([^;]+)','i').exec(document.cookie);if(m)return unescape(m[1]);return "";}
//是否为数字
Navy.IsNumeric=function(v){return /^([+-])?\d+(\.\d+)?$/.test(v);}
//移除html代码
Navy.removeHTML=function(str){return str.replace(/<[^>]+>/g,'');}
//把html代码替换为其实体
Navy.encode=function(str){if(typeof(str)=="string")return str.replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\r/g,'').replace(/\n/g,'<br/>');else return '';}
//从实体还原html代码
Navy.decode=function(str){return str.replace(/&lt;/g,'<').replace(/&gt;/ig,'>').replace(/<br\/?>/ig,'\n');}
//获取文件名称
Navy.getFN=function(){var m=/\/([a-z]+\d*)(?=\.(html|aspx))/i.exec(window.location.href.replace(/(?=[\?|#]).*/,''));if(m)return m[1];else return '';}
